package com.bizprocess;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

public class LCTest {
	@org.junit.Test
	public void test() {
		ProcessEngine engine = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource(
						"temp/activiti.cfg.xml").buildProcessEngine();
		RepositoryService repositoryService = engine.getRepositoryService();
		repositoryService.createDeployment()
				.addClasspathResource("bizprocess/LC-01-01-01_Test.bpmn")
				.deploy();
		TaskService taskService = engine.getTaskService();
		RuntimeService runtimeService = engine.getRuntimeService();
		// 开启流程
		ProcessInstance pi1 = runtimeService
				.startProcessInstanceByKey("testLc");

		Task task = taskService.createTaskQuery()
				.processInstanceId(pi1.getId()).singleResult();
		System.out.println(task.getName());
		Map<String, Object> vars = new HashMap<String, Object>();
		vars.put("before", "manager");
		// 完成“计划主管填写业务信息任务”
		taskService.complete(task.getId(), vars);
		// 完成经营业务经理审查
		task = taskService.createTaskQuery().processInstanceId(pi1.getId())
				.singleResult();
		System.out.println(task.getName());
		vars = new HashMap<String, Object>();
		vars.put("managerPass", "pass");
		vars.put("managerUnPass", "pass");
		taskService.complete(task.getId(), vars);
		
		List<Task> taskList = taskService.createTaskQuery()
				.processInstanceId(pi1.getId()).list();
		for (Task task2 : taskList) {
			// System.out.println(task2.getTaskDefinitionKey());
			if (task2.getTaskDefinitionKey().equals("viceManager")) {
//				vars.put("fuzhongPass", "pass");
				vars.put("fuzhongUnPass", "pass");
				//在这里用代码控制已经审核通过的项目，开启下一段流程
				taskService.complete(task2.getId(), vars);
			} 	
			if (task2.getTaskDefinitionKey().equals("planOfficer")) {
				vars.put("before", "manager");
				taskService.complete(task2.getId(), vars);
			}
			if (task2.getTaskDefinitionKey().equals("planOfficerModify")) {
//				vars.put("before", "manager");
				taskService.complete(task2.getId(), vars);
			}
		}
		taskList = taskService.createTaskQuery().processInstanceId(pi1.getId())
				.list();
		for (Task task2 : taskList) {
			// System.out.println(task2.getName());
			if (task2.getTaskDefinitionKey().equals("planOfficer")) {
				vars.put("before", "fuzhong");
				taskService.complete(task2.getId(), vars);
			} 	
			if (task2.getTaskDefinitionKey().equals("businessManager")) {
				vars.put("managerPass", "pass");
				vars.put("managerUnPass", "pass");
				taskService.complete(task2.getId(), vars);
			}
			if (task2.getTaskDefinitionKey().equals("planOfficerModify")) {
//				vars.put("before", "manager");
				taskService.complete(task2.getId(), vars);
			}
		}
		taskList = taskService.createTaskQuery().processInstanceId(pi1.getId())
				.list();
		for (Task task2 : taskList) {
			System.out.println(task2.getName());
		}
	}

}
